package com.example.myapplication.customizeView.numberInput;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;

import com.example.myapplication.R;

public class InputNumberView extends RelativeLayout {
    private static final String TAG ="InputNumberView" ;
    private int mCurrentNumber = 0;
    private View mMinusBtn;
    private EditText mValueEdt;
    private View mPlusBtn;
    private OnNumberChangeListener mOnNumberChangeListener = null;
    private int mMax;
    private int mMin;
    private int mStep;
    private int mDefaultValue;
    private boolean mDisable;
    private int mBtnBackground;

    public InputNumberView(Context context) {
        this(context,null);
    }

    public InputNumberView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public InputNumberView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        //获取相关属性
        initAttrs(context, attrs);
        initView(context);
       //设置事件
        setUpEvent();

    }

    private void initAttrs( Context context, AttributeSet attrs) {
        TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.InputNumberView);
        mMax = a.getInt(R.styleable.InputNumberView_max, 0);
        mMin = a.getInt(R.styleable.InputNumberView_min, 0);
        mStep = a.getInt(R.styleable.InputNumberView_step, 1);
        mDefaultValue = a.getInt(R.styleable.InputNumberView_defaultValue, 0);
        this.mCurrentNumber = mDefaultValue;

        mDisable = a.getBoolean(R.styleable.InputNumberView_disabled, false);
        mBtnBackground = a.getResourceId(R.styleable.InputNumberView_btnBackground, -1);
        Log.d(TAG,"mMax--==>>"+mMax);
        Log.d(TAG,"mMin--==>>"+mMin);
        Log.d(TAG,"mStep--==>>"+mStep);
        Log.d(TAG,"mDefaultValue--==>>"+mDefaultValue);
        Log.d(TAG,"mDisable--==>>"+mDisable);
        Log.d(TAG,"mBtnBackground--==>>"+mBtnBackground);

        //
        a.recycle();
    }

    private void setUpEvent() {
        mMinusBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mPlusBtn.setEnabled(true);

                mCurrentNumber-=mStep;
                if (mMin != 0 &&mCurrentNumber<=mMin){
                    mCurrentNumber = mMin;
                    v.setEnabled(false);
                    Log.d(TAG,"current is min ...");
                    if (mOnNumberChangeListener != null){
                        mOnNumberChangeListener.onNumberMin(mMin);
                    }

                }
                updateText();

            }
        });
        mPlusBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mMinusBtn.setEnabled(true);

                mCurrentNumber+=mStep;
                if (mMax !=0 &&mCurrentNumber>=mMax){
                    mCurrentNumber = mMax;
                    v.setEnabled(false);
                    Log.d(TAG,"current is max .....");
                    if (mOnNumberChangeListener != null){
                        mOnNumberChangeListener.onNumberMax(mMax);
                    }

                }
                updateText();

            }
        });
    }

    private void initView(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.input_number_view, this, false);
        addView(view);
        mMinusBtn = this.findViewById(R.id.minus_btn);
        mValueEdt = this.findViewById(R.id.value_edt);
        mPlusBtn = this.findViewById(R.id.plus_btn);
            //初始化控件值
        updateText();
        mMinusBtn.setEnabled(!mDisable);
    }

    public int getNumber() {
        return mCurrentNumber;
    }

    public void setNumber(int value) {
        this.mCurrentNumber = value;
        this.updateText();
    }

    public int getmMax() {
        return mMax;
    }

    public void setmMax(int mMax) {
        this.mMax = mMax;
    }

    public int getmMin() {
        return mMin;
    }

    public void setmMin(int mMin) {
        this.mMin = mMin;
    }

    public int getmStep() {
        return mStep;
    }

    public void setmStep(int mStep) {
        this.mStep = mStep;
    }

    public int getmDefaultValue() {
        return mDefaultValue;
    }

    public void setmDefaultValue(int mDefaultValue) {
        this.mDefaultValue = mDefaultValue;
        this.mCurrentNumber = mDefaultValue;
        this.updateText();
    }

    public boolean ismDisable() {
        return mDisable;
    }

    public void setmDisable(boolean mDisable) {
        this.mDisable = mDisable;
    }

    public int getmBtnBackground() {
        return mBtnBackground;
    }

    public void setmBtnBackground(int mBtnBackground) {
        this.mBtnBackground = mBtnBackground;
    }

    /**
     * 更新数值
     */
    private void updateText(){
        mValueEdt.setText(String.valueOf(mCurrentNumber));
        if(mOnNumberChangeListener!=null){
            mOnNumberChangeListener.onNumberChange(this.mCurrentNumber);
        }
    }
    public void setOnNumberChangeListener(OnNumberChangeListener listener){
        this.mOnNumberChangeListener = listener;

    }
    public interface OnNumberChangeListener{
        void onNumberChange(int value);
        void onNumberMax(int maxValue);
        void onNumberMin(int minValue);
    }
}
